﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class Pages_RouteEdit : System.Web.UI.Page
{
	Int64 Route_id { get { return DS.ToInt64(Request.QueryString["Id"]); } }

	#region RouteStationWork
	IList<RouteStationM2M> RouteStationCollection
	{
		get
		{
			IList<RouteStationM2M> rsm2m = (Session["RouteStationCollection"] as IList<RouteStationM2M>) ?? new List<RouteStationM2M>();
			IList<RouteStationM2M> result = new List<RouteStationM2M>();
			foreach (RouteStationM2M item in rsm2m)
				if (item.x)
					result.Add(item);
			return result;
		}
		set
		{
			Session["RouteStationCollection"] = value;
		}
	}

	private void AddRouteStation (RouteStationM2M rs)
	{
		rs.x = true;
		if (rs.Distance == 0)
			rs.Distance = 1;
		IList<RouteStationM2M> rsc = RouteStationCollection;
		rsc.Add(rs);
		RouteStationCollection = rsc;
	}

	private void RemoveRouteStation (Int64 Station_id)
	{
		IList<RouteStationM2M> rsc = RouteStationCollection;
		for (int i = 0; i < rsc.Count; i++)
			if (rsc[i].Station_id == Station_id)
			{
				rsc[i].x = false;
				break;
			}
	}

	private bool SaveRouteStation()
	{
		IList<RouteStationM2M> rsc = (Session["RouteStationCollection"] as IList<RouteStationM2M>) 
										?? new List<RouteStationM2M>();
		bool result = true;
		foreach (RouteStationM2M routeStation in rsc)
		{
			routeStation.Route_id = Route_id;
			result = routeStation.Merge();
		}
		return result;
	}
	#endregion

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			Session["RouteStationCollection"] = null;
			if (Route_id > 0)
				InitUpdateContext();
		}
	}

	private void InitUpdateContext()
	{
		Route r = DBEntity<Route>.Load(Route_id);
		if (r == null)
			Response.Redirect("~/");
		lbSourceStation.Text = (DBEntity<Station>.Load(r.SourceStation_id) ?? new Station()).Name;
		hfSourceStation.Value = r.SourceStation_id.ToString();
		lbTargetStation.Text = (DBEntity<Station>.Load(r.TargetStation_id) ?? new Station()).Name;
		hfTargetStation.Value = r.TargetStation_id.ToString();
		tbRouteName.Text = r.Name;
		RouteStationM2M rsM2M = new RouteStationM2M();
		rsM2M.Route_id = Route_id;
		RouteStationCollection = DBEntity<RouteStationM2M>.Select();
		rptRouteStation.DataSource = RouteStationCollection;
		rptRouteStation.DataBind();
	}

	protected void OnRouteSave(object s, EventArgs e)
	{
		Route r = DBEntity<Route>.Load(Route_id) ?? new Route();
		r.SourceStation_id = DS.ToInt64(hfSourceStation.Value);
		r.TargetStation_id = DS.ToInt64(hfTargetStation.Value);
		r.Name = tbRouteName.Text;
		if (r.Merge() && SaveRouteStation())
		{
		}
	}

	protected void OnEditStation(object s, EventArgs e)
	{
		hfModalAction.Value = (s as LinkButton).CommandName;
		tbSearchStation.Text = string.Empty;
		rptStation.DataSource = null;
		rptStation.DataBind();
		lkbAddNewStation.Visible = false;
		//MPE.Show();
	}

	protected void OnSearchStation(object s, EventArgs e)
	{
		Station station = new Station();
		station.Name = tbSearchStation.Text;
		rptStation.DataSource = DBEntity<Station>.Select(station);
		lkbAddNewStation.Visible = ((rptStation.DataSource as IList<Station>).Count == 0);
		rptStation.DataBind();
	}

	protected void OnUpdateRoute(Station s)
	{
		if (hfModalAction.Value == "SourceStation")
		{
			lbSourceStation.Text = s.Name;
			hfSourceStation.Value = s.Id.ToString();
			upRoute.Update();
		}
		if (hfModalAction.Value == "TargetStation")
		{
			lbTargetStation.Text = s.Name;
			hfTargetStation.Value = s.Id.ToString();
			upRoute.Update();
		}
		if (hfModalAction.Value == "RouteStation")
		{
			RouteStationM2M rs = new RouteStationM2M();
			rs.Station_id = s.Id;
			rs.Station_name = s.Name;
			AddRouteStation(rs);
			rptRouteStation.DataSource = RouteStationCollection;
			rptRouteStation.DataBind();
			upRoute.Update();
		}
	}

	protected void OnAddNewStation(object s, EventArgs e)
	{
		Station station = new Station();
		station.Name = tbSearchStation.Text;
		if (station.Merge())
			OnUpdateRoute(station);
	}

	protected void AddStation(object s, EventArgs e)
	{
		Station station = DBEntity<Station>.Load(DS.ToInt64((s as LinkButton).CommandArgument));
		OnUpdateRoute(station);
	}

	protected void OnDeleteStation(object s, EventArgs e)
	{
		RemoveRouteStation(DS.ToInt64((s as LinkButton).CommandArgument));
		rptRouteStation.DataSource = RouteStationCollection;
		rptRouteStation.DataBind();
		upRoute.Update();
	}

	protected void lbEditDistances_Click(object sender, EventArgs e)
	{
		Response.Redirect(ResolveUrl("~/Pages/EditDistances.aspx?id=" + Route_id.ToString()));
	}
}
